package com.leyou.item.web;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.aspectj.apache.bcel.classfile.Module;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import sun.awt.SunHints;

import java.util.List;

@RequestMapping("spec")
@RestController
public class SpecController {
    private SpecGroupService groupService;
    private SpecParamService paramService;

    public SpecController(SpecGroupService groupService,SpecParamService paramService){
        this.groupService=groupService;
        this.paramService=paramService;
    }

    /**
     * 根据商品分类id，查询规格组的集合
     * @param cid
     * @return
     */
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>>queryGroupByCategory(@RequestParam("id") Long cid){

        return ResponseEntity.ok(this.groupService.queryGroupByCategory(cid));
}

    /**
     * 根据条件查询规格参数集合
     * @param categoryId
     * @param groupId
     * @param searching
     * @return
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParams(
            @RequestParam(value = "categoryId" ,required = false)Long categoryId,
            @RequestParam(value = "groupId" ,required = false)Long groupId,
            @RequestParam(value = "searching" ,required = false)Boolean searching

    ){
        return ResponseEntity.ok(paramService.queryParams(categoryId,groupId,searching));
    }

    /**
     * 根据分类id查询规格组及组内参数
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>>querySpecList(@RequestParam("id") Long categoryId){
        return ResponseEntity.ok(groupService.querySpecList(categoryId));
    }

    /**
     * 新增规格组
     * @param groupDTO
     * @return
     */
    @PostMapping("/group")
    public ResponseEntity<Void> saveGroup(@RequestBody SpecGroupDTO groupDTO){
        this.groupService.saveGroup(groupDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改规格组
     * @param groupDTO
     * @return
     */
    @PutMapping("/group")
    public ResponseEntity<Void>updateGroup( @RequestBody SpecGroupDTO groupDTO){

        groupService.updateGroup(groupDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 新增规格参数
     * @param paramDTO
     * @return
     */
    @PostMapping("/param")
    public ResponseEntity<Void> saveParam(@RequestBody SpecParamDTO paramDTO){
        this.paramService.saveParam(paramDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改规格参数
     * @param paramDTO
     * @return
     */
    @PutMapping("/param")
    public ResponseEntity<Void> updateParam(@RequestBody SpecParamDTO paramDTO){
        this.paramService.updateById(paramDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
